package com.zprmb.udzt;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.address.AddressWheel;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 添加收货地址
 * @author shenshao
 *
 */
public class Add_address extends BaseActivity implements OnClickListener{
	private TextView et_address,title;
	private ImageView img_back;
	private Button btn_confirm;
	private EditText et_name,et_tel,et_zip_code,et_street,et_de;
	private String province,city,area,code;
	private RelativeLayout rl_back,rl_switch;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_address);
		BbtApplication.getInstance().addActvity(this);
		et_address=(TextView)findViewById(R.id.et_address);
		img_back=(ImageView)findViewById(R.id.img_back);
		title=(TextView)findViewById(R.id.title);				
		et_name=(EditText)findViewById(R.id.address_name);
		et_street=(EditText)findViewById(R.id.et_street);
		et_de=(EditText)findViewById(R.id.deatil_address);
		rl_switch=(RelativeLayout)findViewById(R.id.rl_switch);
		et_tel=(EditText)findViewById(R.id.address_tel);
		et_zip_code=(EditText)findViewById(R.id.address_postcode);
		btn_confirm=(Button)findViewById(R.id.btn_confirm);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		if(getIntent().getIntExtra("update", 0)==1){
			title.setText("修改收货地址");
			et_name.setText(getIntent().getStringExtra("name"));
			et_tel.setText( getIntent().getStringExtra("tel"));
			et_address.setText( getIntent().getStringExtra("address"));
			et_de.setText( getIntent().getStringExtra("detail"));
			btn_confirm.setText("保存");
			province=getIntent().getStringExtra("province");
			city=getIntent().getStringExtra("city");
			area=getIntent().getStringExtra("area");
		}else{
			title.setText("新建收货地址");
		}
		rl_switch.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		et_address.setOnClickListener(this);
		img_back.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
	
			if(!(arg2.getStringExtra("data")==null)){
				
				et_address.setText(arg2.getStringExtra("data"));
				province=arg2.getStringExtra("province");
				city=arg2.getStringExtra("city");
				area=arg2.getStringExtra("area");
				code=arg2.getStringExtra("code");
				et_zip_code.setText(code);
			}
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.et_address:
			Intent intent=new Intent(Add_address.this,AddressWheel.class);
			startActivityForResult(intent, 0);
			overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.slide_out_to_top);
			break;
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;
		case R.id.rl_switch:
			
			break;
		case R.id.btn_confirm:
			if(et_name.getText().toString().equals("")){
				Toast.makeText(Add_address.this, "收货人姓名不能为空",2000).show();
				return;
			}
//			if(Regular.hasSpecialCharacter(et_name.getText().toString())){
//				Toast.makeText(Add_address.this, "收货人姓名不合法",2000).show();
//				return;
//			}
			if(et_address.getText().toString().equals("")){
				Toast.makeText(Add_address.this, "收货人地址",2000).show();
				return;
			}
			if(et_de.getText().toString().equals("")){
				Toast.makeText(Add_address.this, "详细地址不能为空",2000).show();
				return;
			}
			
			if(et_tel.getText().toString().equals("")){
				Toast.makeText(Add_address.this, "收货人电话不能为空",2000).show();
				return;
			}
			if(getIntent().getIntExtra("update", 0)==1){
				update();
			}else{
				btn_confirm.setEnabled(false);
				add();
			}
			
			break;
		
		}
	}
	private void update() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		try {
			params.put("receiveName",URLEncoder.encode(et_name.getText().toString(), "UTF-8"));
			params.put("province",  URLEncoder.encode(province,"utf-8"));
			params.put("city", URLEncoder.encode(city, "UTF-8"));
			params.put("area",  URLEncoder.encode(area, "UTF-8"));
			params.put("expressAddr",URLEncoder.encode(et_de.getText().toString(), "UTF-8") );
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		params.put("addressId", getIntent().getStringExtra("id"));
		params.put("receivePhone", et_tel.getText().toString());
		params.put("postalcode", et_zip_code.getText().toString());
		
		
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("dizhi", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					
					
					
					Intent mIntent = new Intent();
	                mIntent.setAction("exit");
	                sendBroadcast(mIntent);
					Add_address.this.finish();
				}else {
					
					if (jsonObject.optString("comment").equals("please login")) {
						
						
						if(getShare().getString("mobile", "").equals("")){
							Intent intent = new Intent(Add_address.this,Log_or_RegisActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}else{
							
							Intent intent = new Intent(Add_address.this,LoginActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}
						
					}else{
						Toast.makeText(Add_address.this, jsonObject.optString("comment"), 2000).show();
						Add_address.this.finish();
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				btn_confirm.setEnabled(true);
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(Add_address.this, Constant.URL+"/api/v5/user/saveAdr?", params.toString());
	}
	@SuppressWarnings("deprecation")
	private void add() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		try {
			params.put("receiveName",URLEncoder.encode(et_name.getText().toString(), "UTF-8"));
			params.put("province",  URLEncoder.encode(province,"utf-8"));
			params.put("city", URLEncoder.encode(city, "UTF-8"));
			params.put("area",  URLEncoder.encode(area, "UTF-8"));
			params.put("expressAddr",URLEncoder.encode(et_de.getText().toString(), "UTF-8") );
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		params.put("receivePhone", et_tel.getText().toString());
		params.put("postalcode", et_zip_code.getText().toString());
		
		
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("dizhi", jsonObject.toString());
				btn_confirm.setEnabled(true);
				if(jsonObject.optInt("success")==1){
					Add_address.this.finish();
				}else {
					btn_confirm.setEnabled(true);
					if (jsonObject.optString("comment").equals("please login")) {
						
						
						if(getShare().getString("mobile", "").equals("")){
							Intent intent = new Intent(Add_address.this,Log_or_RegisActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}else{
							
							Intent intent = new Intent(Add_address.this,LoginActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}
						
					}else{
						Toast.makeText(Add_address.this, jsonObject.optString("comment"), 2000).show();
						Add_address.this.finish();
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				btn_confirm.setEnabled(true);
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(Add_address.this, Constant.URL+"/api/v5/user/addAdr?", params.toString());
		
	}

}
